home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-11-19 | 3.8 KB | 107 lines | [TEXT/MPS ] |
- /*
- File: TTokenExample.cp
-
- Contains: This module shows an example use of the TToken class
-
- Copyright: © 1993 by Apple Computer, Inc., all rights reserved.
-
- */
-
- #include "TInitSLM.h" // the TInitSLM class and SLM include files
-
- #include "TTokenExample.h"
-
- ///————————————————————————————————————————————————————————————————————————————————————
- /// CONSTANTS
- ///————————————————————————————————————————————————————————————————————————————————————
-
- // The id we will use for our buffer token
- #define TBufferTokenID "exam:tokn$Buff"
-
- ///————————————————————————————————————————————————————————————————————————————————————
- /// GLOBALS
- ///————————————————————————————————————————————————————————————————————————————————————
-
- TArbitrator *gArbitrator = nil;
-
- ///————————————————————————————————————————————————————————————————————————————————————
- /// PROTOTYPES
- ///————————————————————————————————————————————————————————————————————————————————————
-
- static void write_to_buffer( TBufferToken *theToken, char *string );
-
- /*————————————————————————————————————————————————————————————————————————————————————
- main
-
- This example demonstrate the use of TToken for accessing a buffer for reading and
- writting to the buffer. First, it creates and then registers the token so it can
- be accessed by other clients. After being registered, it request exclusive access
- to the buffer. If successful, it will write to that buffer.
-
- Note that usually you don't need to create a token when registering an object
- with the arbitrator. The usual method is to simply call RegisterObject and let
- the arbitrator create the token for you. If however, you want your token to be
- more then just a simple TToken, you can subclass it and call RegisterToken
- instead as is done in this example. In this example the token acts like a
- semaphore to the buffer.
- ————————————————————————————————————————————————————————————————————————————————————*/
-
- main()
- {
- TInitSLM initLibraryManager; // initialize the shared library manager
-
- if( initLibraryManager.Failed() ) // if we failed, let go home
- return 1;
-
- TBufferToken* mytoken = nil;
-
- TRY
- gArbitrator = new TArbitrator; // create an arbitration
- FailNULL( gArbitrator, ErrorCode(), "Sorry failed to create an arbitrator" );
-
- mytoken = new TBufferToken; // create an instance of our TBufferToken
- FailNULL( mytoken, ErrorCode(), "Sorry failed to create a Token" );
-
- mytoken->SetID( TBufferTokenID ); // set our tokens id
-
- gArbitrator->RegisterToken(mytoken);// Register token with our arbitrator
-
- cout << "Registered the token = " << (void *)mytoken << endl;
-
- write_to_buffer(mytoken, "Hello "); // write to the buffer
-
- mytoken->Release(); // Release the token
-
- write_to_buffer(mytoken, " World"); // write to buffer, no problem here
-
- write_to_buffer(mytoken, " This should fail"); // write to buffer, this should fail
- ENDTRY
-
- delete mytoken;
- delete gArbitrator;
- return 0;
- }
-
- /*————————————————————————————————————————————————————————————————————————————————————
- write_to_buffer
-
- Get exclusive access to the token and then writes string to the buffer.
- For the sake of this example we don't release the example. It is up to
- the caller to do that or the next call to write_to_buffer will fail.
- ————————————————————————————————————————————————————————————————————————————————————*/
- static void write_to_buffer( TBufferToken *theToken, char *string )
- {
- // get an exclusive access to the buffer
-
- TBufferToken *exctoken = (TBufferToken *)gArbitrator->GetToken( theToken->GetID(),
- kExclusiveTokenRequest );
-
- if( exctoken ) {
- cout << "Got exclusive access to " << TBufferTokenID" token" << endl;
- exctoken->BufWrite( string );
- exctoken->BufRead(); // read what we wrote so far
- }
- else
- cout << "Sorry, unable to get access to the token" << endl;
-
- }